<?php

namespace App\Http\Controllers\H5\Shop;

use App\Common\Exceptions\AixinException;
use App\Common\Exceptions\AixinExceptionCode;
use App\Common\Model\ShopAccount;
use App\Common\Service\TouchLayerService;
use App\Http\Controllers\Controller;
use App\Model\Btree;
use App\Service\TouchService;
use Illuminate\Http\Request;

class TouchController extends Controller
{
    protected $touchLayerService;

    /**
     * TouchController constructor.
     * @param $touchLayerService
     */
    public function __construct(TouchLayerService $touchLayerService)
    {
        $this->touchLayerService = $touchLayerService;
    }

    /**
     * 量碰管理首页
     * @param Request $request
     * @param TouchService $touchService
     * @return array
     */
    public function index(Request $request, TouchService $touchService)
    {
        $uid = $request->uid;
        $touchInfo = $touchService->getTouchInfo($uid);
        return ApiResponse($touchInfo);
    }

    /**
     * 量碰管理->左右去销售统计
     * @param Request $request
     * @param TouchService $touchService
     * @return array
     */
    public function sales(Request $request, TouchService $touchService)
    {
        $uid = $request->uid;
        $year = $request->year ? $request->year : date('Y');
        $touchInfo = $touchService->getSalesInfo($uid, $year);
        return ApiResponse($touchInfo);
    }

    /**
     * 量碰管理->左右区每天销售统计
     * @param Request $request
     * @param TouchService $touchService
     */
    public function daySales(Request $request, TouchService $touchService)
    {
        $uid = $request->uid;
        $year = $request->year;
        $month = $request->month;
        $day = $request->day ? $request->day : 0;
        $zone = $request->zone ? $request->zone : 'l';
        $start = $request->start ? $request->start : 0;
        $count = $request->count ? $request->count : 2;
        $daySale = $touchService->getDaySale($uid, $year, $month, $day, $zone, $start, $count);
        return ApiResponse($daySale);
    }

    public function test()
    {
        $service = new TouchService();
//        $yeterday  = mktime(0, 0, 0, date("m")  , date("d") - 1, date("Y"));
//        $today = strtotime(date('Y-m-d'));
        $begin = mktime(24, 0, 0, 4, 0, 2018);
        $end = mktime(24, 0, 0, 5, 0, 2018);
        $btreeModel = new Btree();
        $children = $btreeModel->getLimitLRChildren(45);
        $re = $service->aggregateSales($children['lzone'], $begin, $end);
        dd($re);

    }
    public function test2(){
        $res = $this->touchLayerService->collisionLayer(468);
        if (!$res){
            dd($this->touchLayerService->getError());
        }
    }

    /**
     * 用户层碰收益总额
     * @param Request $request
     * @return array
     * @throws AixinException
     */
    public function touchLayer(Request $request){
        $uid = $request->get('uid');
        $data = $this->touchLayerService->touchLayerInfo($uid);
        if (empty($data)){
            throw new AixinException(AixinExceptionCode::RESULT_NULL);
        }
        return ApiResponse($data);
    }
}
